const BASE_URL = 'https://music-api.heheda.top';
/**
 * 发送请求
 */
export default function request(api, method, data)
{
  let header = {'content-type': 'application/json'};

  return new Promise((reslove, reject) => {
	uni.showLoading({title: '加载中'});
    uni.request({
      url: BASE_URL + '/' + api,
      method: method || 'GET',
      header: header,
      data: data || {},
      success: (res) => {
		if (res.data.code == 200)
			reslove(res.data, res);
		else if (res.data.code == 402)
			reslove(res.data, res);
		else if (res.data.code == 0) {
			reslove(res.data, res);
		}
		else if (res.data.code == "0") {
			reslove(res.data, res);
		}
		else 
			reject(res.data.msg || '系统错误');
        
		uni.hideLoading();
      },
      fail: (msg) => {
        reject('请求失败');
      }
    })
  });
}

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  request[method] = (api, data, opt) => request(api, method, data, opt || {})
});

